<?php
namespace org\FoxNet;
use RuntimeException;

/***
主动的情况:
  1. web后台给代理开俱乐部;
  2. 俱乐部人员管理(增/改/查);

自动的情况
  1. 扫推广码         -> 绑定代理关系 && 自动创建俱乐部关系;
  2. 客户端输入邀请码 -> 绑定代理关系 && 自动创建俱乐部关系;

*/
class ClubManager extends Core {

  /**
   *  @brief  : 发送请求-用户注册
   *  @return : none
   *  @details: 成功将回调, 失败丢出异常
   */
  public function req_RegisterUser($req, $callback = null) {

    $pkg = array_merge([
      'UserID'  => 0,
      'GameID'  => 0,
      'NickName'=> '',
      'Avatar'  => '',
    ], $req);

    $buf = pack('VVa64a400', // CMD_GP_REGISTERUSER 用户注册
      $pkg['UserID'], // DWORD dwUserID; //userid
      $pkg['GameID'], // DWORD dwGameID; //gameid
      self::UTF8_UNICODE($pkg['NickName']), // TCHAR szNickName[32];  //用户昵称
      self::UTF8_UNICODE($pkg['Avatar']) // TCHAR szWxHeadurl[200]; //微信头像
    );

    $this->SendData(33, 3308, $buf); // 33-3308 SUB_GP_REGISTERUSER 用户注册
    // 可能会收到如下4种包
    // 33-3363 SUB_GP_MYCREATEGROUP     自己创建群信息 不一定有
    // 33-3387 SUB_GP_GROUPALLINFO      俱乐部房间信息 不一定有
    // 33-3361 SUB_GP_GETUSERMESSAGE    通知用户消息  不一定有
    // 33-3379 SUB_GP_MYGROUPLISTFINSH  列表消息完成  必定有&& 结束包
    $finsh = false;
    $retn = [];
    for ($ecx = 0; $ecx < self::UMAX; $ecx++) {
      $msgs = $this->RecvMsgs();
      printf(">>>>>>[%d]\n", $ecx);
      print_r($msgs);
      foreach($msgs as $msg) {
        $MCmdID = $msg['head']['MainCmdID'];
        $SCmdID = $msg['head']['SubCmdID'];
        if ($MCmdID == 33) {
          if ($SCmdID == 3363) { // GroupInfoEx
            $eax = unpack('VGroupID/CByte/a64GroupBossNickName/a400GroupBossWxHeadUrl/VUserID/VGameID/a64GroupName/CDrawCountLimit/CJoinGamePeopleCount/CPayMode/a100GameRule/VGroupUserCnt/VGroupRoomCnt', $msg['body']);
            $eax['GroupBossNickName'] = trim(self::UNICODE_UTF8($eax['GroupBossNickName']));
            $eax['GroupBossWxHeadUrl'] = trim(self::UNICODE_UTF8($eax['GroupBossWxHeadUrl']));
            $eax['GroupName'] = trim(self::UNICODE_UTF8($eax['GroupName']));
            $retn = $eax;
          } else if ($SCmdID == 3379) {
            $finsh = true;
            break;
          } else {
            //
          }
        }
      }
      if ($finsh) {
        break;
      }
      usleep(self::USUS);
    }
    if (!$finsh) {
      throw new RuntimeException(sprintf('接收消息等待超时%d秒', self::USUS * self::UMAX));
    }
    if (is_callable($callback)) {
      call_user_func($callback, $retn);
    }
  }
  
  /**
   *  @brief  : 创建俱乐部
   *  @return :
   *  @details: 只有推广员拥有该操作
   */
  public function req_CreateGroup($req, $callback = null) {

    $pkg = array_merge([
      'UserID' => 0,
      'GameID' => 0,
      'GroupName' => '',
    ], $req);

    $buf = pack('VVa64CCCa100', // CMD_GP_CREATEGROUP 创建群
      $pkg['UserID'],  // DWORD dwUserID; // 群主ID,
      $pkg['GameID'],  // DWORD dwGameID; // 群主GameID
      self::UTF8_UNICODE($pkg['GroupName']), // TCHAR GroupName[32]; // 群名称
      0,  // BYTE  cbDrawCountLimit;      // 局数选择 Index 0-5
      0,  // BYTE  cbJoinGamePeopleCount; // 参与游戏的人数 0-6
      0,  // BYTE  cbPayMode;             // 付费模式       1
      0   // BYTE  cbGameRule[100];  // 游戏规则 弟 0 位标识 是否设置规则 0 代表未设置 1 代表设置
    );

    $this->SendData(33, 3300, $buf);  // 33-3300 SUB_GP_CREATEGROUP 创建群
    // 可能会收到如下2种包
    // 33-3351 SUB_GP_CREATEGROUPSUCCESS 创建成功
    // 33-3353 SUB_GP_CREATEGROUPFIAL 创建失败
    $finsh = false;
    $retn = [];
    for ($ecx = 0; $ecx < self::UMAX; $ecx++) {
      printf("====%d\n", $ecx);
      $msgs = $this->RecvMsgs();
      print_r($msgs);
      foreach($msgs as $msg) {
        $MCmdID = $msg['head']['MainCmdID'];
        $SCmdID = $msg['head']['SubCmdID'];
        if ($MCmdID == 33) {
          if ($SCmdID == 3351) {  // CMD_GP_CREATESUCCESS
            var_dump(strlen($msg['body']));
            $eax = unpack('VGroupID/a64GroupBossNickName/VUserID/VGameID/a64GroupName/CDrawCountLimit/CJoinGamePeopleCount/CPayMode/a100GameRule', $msg['body']);
            var_dump($eax);
            $retn = $eax;
            $finsh = true;
            break;
          } else if ($SCmdID == 3353) {
            $finsh = true;
            break;
          } else {
            //
          }
        }
      }
      if ($finsh) {
        break;
      }
      usleep(100000);
    }
    if (!$finsh) {
      throw new RuntimeException(sprintf('接收消息等待超时%d秒', self::USUS * self::UMAX));
    }
    if (is_callable($callback)) {
      call_user_func($callback, $retn);
    }
  }
  
  /**
   *  @brief  : 加入群
   *  @return :
   *  @details:
   */
  public function req_JoinGroup($req) {
    $pkg = array_merge([
      'GroupID' => 0,
      'UserID'  => 0,
    ], $req);

    $buf = pack('VV', // CMD_GP_JOINGROUP
      $pkg['GroupID'],  // DWORD dwJoinGroupID;
      $pkg['UserID']    // DWORD dwUserID; //用户ID
    );

    $this->SendData(33, 3304, $buf);  // 33-3304 SUB_GP_JOINGROUP 加入群
    $msgs = $this->RecvMsgs();
    print_r($msgs);


  }

} // END CLASS
